package com.lashou.labelutil.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.lashou.labelutil.model.GoodsCategoryLabelLog;
import com.lashou.labelutil.model.GoodsCategoryList;
import com.lashou.labelutil.model.GoodsProfileBean;
import com.lashou.labeutil.util.TimeUtil;

public class LabelServiceImpl extends BaseService implements ILabelService {
	private static List<GoodsProfileBean> goodsprofiles = new ArrayList<GoodsProfileBean>();
	private static List<GoodsCategoryList> goodscategorys = new ArrayList<GoodsCategoryList>();
	
	@Override
	public GoodsProfileBean findLastGoodsProfileBean() {
		if(goodsprofiles.size()>0)
		{
			
			for(int i=0;i<goodsprofiles.size();i++)
			{
				GoodsProfileBean bean = goodsprofiles.get(i);
				if(bean.getEditStaut()==0)
				{
					goodsprofiles.get(i).setEditStaut(1);
					bean = this.getLabelDAO().findGoodsProfileBeanById(bean.getGoodsId());
					return bean;
				}
			}
			return null;
		}
		else
		{
			return null;
		}
	}

	@Override
	public synchronized boolean checkLabelStaut() {
		if(goodsprofiles.size()>0)
		{
			System.out.println("目前可编辑的信息记录数为："+goodsprofiles.size());
			for(GoodsProfileBean b:goodsprofiles)
			{
				if(b.getEditStaut() == 0)
				{
					return true;
				}
			}
			
		}
		
		if(this.getLabelDAO().getGoodsProfileTotal()==this.getLabelDAO().getGoodsLabelLogTotal())
		{
			return false;
		}
		System.out.println("清理缓存数据。。。。"+goodsprofiles.size());
		goodsprofiles.clear();
		System.out.println("加载最新需要编辑信息.......");
		List<GoodsProfileBean> goods = this.getLabelDAO().findAllGoodsProfileBean();
		Map<String,GoodsCategoryLabelLog> logs = this.getLabelDAO().findAllGoodsCategoryLabelLog();
		for(GoodsProfileBean bean:goods)
		{
			if(!logs.containsKey(bean.getGoodsId()+""))
			{
				goodsprofiles.add(bean);
			}
			
		}	
		System.out.println("需要编辑的信息总数为 :"+goodsprofiles.size());
		goodscategorys.clear();
		goodscategorys = this.findAllGoodsCategoryList();
		return true;
	}

	@Override
	public boolean closeEditLabel(GoodsProfileBean goodsprofile) {
		if(goodsprofiles.size()==0)
		{
			return true;
		}
		String title = "";//(goodsprofiles.get(0).getTitle()==null?"":goodsprofiles.get(0).getTitle());
		for(GoodsProfileBean bean:goodsprofiles)
		{
			if((goodsprofile.getGoodsId()+"").equals(bean.getGoodsId()+""))
			{
				title = bean.getTitle();
			}
		}
		
		System.out.println("当前标记goodsId ："+goodsprofile.getGoodsId()+"  title:"+title);
		//getall goods
		List<GoodsProfileBean> editgoods = new ArrayList<GoodsProfileBean>();
		
		for(GoodsProfileBean goods:goodsprofiles)
		{
			if(null != title && title.equals(goods.getTitle()))
			{
				editgoods.add(goods);
			}
			
		}
		
		if(null == title )
		{
			System.out.println("标题为空 ......");
			editgoods.add(goodsprofile);
		}
		System.out.println("标题相同goods size:"+editgoods.size());
		for(GoodsProfileBean bean:editgoods)
		{
			goodsprofile.setGoodsId(bean.getGoodsId());
			boolean ok = this.getLabelDAO().updateGoodsProfileBean(goodsprofile);
			System.out.println("更新记录:"+goodsprofile.getGoodsId());
			if(ok)
			{
				GoodsCategoryLabelLog log = new GoodsCategoryLabelLog();
				log.setGoodsId(goodsprofile.getGoodsId());
				log.setCategoryLogs(goodsprofile.getCategory());
				this.getLabelDAO().saveGoodsCategoryLabelLog(log);
				goodsprofiles.remove(bean);
			}
		}
		return true;
	}

	@Override
	public List<GoodsCategoryList> findAllGoodsCategoryList() {
		
		if(goodscategorys.size()==0)
		{
			List<GoodsCategoryList> list = this.getLabelDAO().findAllGoodsCategoryList(); 
			for(GoodsCategoryList bean:list)
			{
				if(bean.getGoodsCategoryId()>=100)
				goodscategorys.add(bean);
			}
		}
		return goodscategorys;
	}

	@SuppressWarnings("static-access")
	@Override
	public int getRemainCount() {

		try
		{
			return this.goodsprofiles.size();
		}catch(Exception e)
		{
			return 0;
		}
		
	}

	@Override
	public List<GoodsProfileBean> findGoodsProfileByCategoryType(
			int categoryType, int startIndex, int count) {
		try
		{
			
			
			return this.getLabelDAO().findGoodsProfileByCategoryType(categoryType+"", startIndex, count);
			
			/*if(-1 == categoryType)
			{
				return this.getLabelDAO().findGoodsProfileByCategoryType("-1", startIndex, count);
			}
			else
			{
				return this.getLabelDAO().findGoodsProfileByCategoryType(categoryType+"", startIndex, count);
			}*/
		
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
		
		
	}

	@Override
	public int findGoodsProfileTotal(int categoryType) {
		try
		{
			String sql = "select count(c.cu) from ( select count(goods_id) cu from goods_profile "+(categoryType==-1?"":"where category like '%"+categoryType+"%'")+" group by title ) c";
			if(-2 == categoryType)
			{
				sql = "SELECT count(goods_id) from goods_profile where category is null or category  = '' or category  = '0;' or category  = '1;' or category  = '2;' or category  = '3;' or category  = '4;'";
			}
			
			return this.getLabelDAO().findBySQL(sql);
		}catch(Exception e)
		{
			e.printStackTrace();
		return 0;
		}
	}

	@Override
	public GoodsProfileBean findGoodsProfileBeanById(Integer goodsId) {
		return this.getLabelDAO().findGoodsProfileBeanById(goodsId);
	}

	@Override
	public List<GoodsProfileBean> findGoodsProfileBeanByTitle(String title) {
		return this.getLabelDAO().findGoodsProfileBeanByTitle(title);
	}

	@Override
	public boolean updateGoodsProfileBean(GoodsProfileBean goodsProfile) {

		return this.getLabelDAO().updateGoodsProfileBean(goodsProfile);
	}

	
	@Override
	public List<GoodsProfileBean> findLastProfileByDate(String date,
			int startIndex, int count) {
		long time = 0;
		if(null != date && !"".equals(date))
		{
			java.util.Date dt = TimeUtil.getyMdHms(date);
			if(dt != null)
			{
				time = dt.getTime();
			}
		}
		
		return this.getLabelDAO().findLastGoodsProfile(time, startIndex, count);
	}

	@Override
	public int findTotalLastProfilebyDate(String date) {
		long time = 0;
		if(null != date && !"".equals(date))
		{
			java.util.Date dt = TimeUtil.getyMdHms(date);
			if(dt != null)
			{
				time = dt.getTime();
			}
		}
		return this.getLabelDAO().findTotalLastGoodsProfile(time);
	}
	
	

}
